<?php
/**
 * 栏目类
 */
namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\model\Columnmodel;
use app\admin\validate\Column as validate;

class Column extends Common
{
    protected $columnModel = '';

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        parent::__construct($app);
        $this->columnModel = new Columnmodel();
    }

    //栏目列表页
    public function index()
    {
		//dump(env('test.hello', ''));
        $res = $this->columnModel->findAllInfo();

        $cloumn = array();
        foreach($res as $key=>$val){
            if(!$val['father_id']){
                $cloumn[1][] = $val;
            }else{
                $cloumn[$val['level']][$val['father_id']][] = $val;
            }
        }
        View::assign(['cloumn'=>$cloumn, 'i'=>1]);
        return view();
    }

    //新增/编辑
    public function add(){
        $id = $this->request->get('id', 0);

        $firstColumnList = $this->columnModel->findAllInfo('id, name', [['level', '=', 1], ['status', '=', 1]]);
        View::assign(['firstColumnList'=>$firstColumnList]);

        if($id){
            $columnInfo = $this->columnModel->findDetaiInfoById('*', $id);

            //根据fid获取相关信息
            if($columnInfo['father_id']){
                $columnData = $this->columnModel->findDetaiInfoById('level, father_id', $columnInfo['father_id']);
                if($columnData['level'] == 2){
                    $columnInfo['first_id'] = $columnData['father_id'];
                    $columnInfo['second_id'] = $columnInfo['father_id'];

                    //获取子栏目信息
                    $columnInfo['columndata'] =$this->columnModel->findChildCloumnByFid($columnInfo['first_id']);
                }else{
                    $columnInfo['first_id'] = $columnInfo['father_id'];
                    $columnInfo['second_id'] = 0;
                }
            }else{
                $columnInfo['first_id'] = $columnInfo['second_id'] = 0;
            }

            $columnInfo['action_id'] = explode(',', $columnInfo['action_id']);

            View::assign(['cloumnInfo'=>$columnInfo, 'id'=>$id]);
            return view('edit');
        }else{
            return view('add');
        }
    }

    //保存
    public function save(){
        $dataArr = $this->request->request('column/a', []);
        $id = $this->request->post('id', 0);

        //获取father_id
        if($dataArr['second_id']){
            $dataArr['level'] = 3;
            $dataArr['father_id'] = $dataArr['second_id'];
        }else if($dataArr['first_id']){
            $dataArr['level'] = 2;
            $dataArr['father_id'] = $dataArr['first_id'];
        }else{
            $dataArr['level'] = 1;
            $dataArr['father_id'] = 0;
        }
        unset($dataArr['second_id'], $dataArr['first_id']);

        if($msg = $this->checkData($dataArr))  return json(['result'=>'fail', 'title'=>$msg]);
        $dataArr['action_id'] = $dataArr['action_id'] ? implode(',', $dataArr['action_id']) : '';

        $res = $this->columnModel->saveInfo($dataArr, $id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        //$this->setColumnData(true);
        event('ChangeRoleColumn');
        $this->logsData = ['cid'=>$id ? $id : $res, 'action'=>$id ? 'edit' : 'add', 'msg'=>$dataArr['name']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url('Column/index')]);
    }

    //删除栏目
    public function delColumn(){
        $id = $this->request->post('id', 0);

        if(!$id)  return json(array('result'=>'fail', 'title'=>'参数不能为空！'));

        //查询栏目下是否还有下级栏目
        $res = $this->columnModel->findChildCloumnByFid($id);

        if($res)  return json(array('result'=>'fail', 'title'=>'该栏目下还有其他栏目，无法删除！'));

        //删除栏目
        $delRes = $this->columnModel->delColumn($id);
        if(!$delRes)  return json(array('result'=>'fail', 'title'=>'操作失败！'));

        //$this->setColumnData(true);
        event('ChangeRoleColumn');
        $this->logsData = ['cid'=>$id, 'action'=>'del'];
        return json(array('result'=>'success'));
    }

    //异步获取栏目
    public function ajaxGetColumnByLevel(){
        $fid = $this->request->post('id', 0);

        $columnList = $this->columnModel->findAllInfo('*', [['father_id', '=', $fid], ['status', '=', 1]]);

        return json(['result'=>'success','columnList'=>$columnList, 'num'=>count($columnList)]);
    }
}
